package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import dao.DAOContact;
import domain.Contact;
import domain.Entreprise;

/**
 * Servlet implementation class SearchContact
 */
public class SearchContactServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchContactServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String idGroud = request.getParameter("nameGroup");

		doGetOrPost(request, response, idGroud);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGetOrPost(request, response, "");
	}

	protected void doGetOrPost(HttpServletRequest request,
			HttpServletResponse response, String idGroup)
			throws ServletException, IOException {

		List<Contact> contacts = new ArrayList<Contact>();
		// Contact[] contacts = null;
		ApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());
		DAOContact dao = (DAOContact) context.getBean("contactDAO");

		if (idGroup != null && !idGroup.isEmpty()) {

			contacts = dao.searchByGroupID(idGroup);
			// for (Contact contact : cts) {
			// contacts.add(contact);
			// }

		} else {

			String critere = (String) request.getParameter("typeFind");
			String key = (String) request.getParameter("key");
			contacts = requestByCriteria(dao, critere, key);
		}

		request.setAttribute("listContact", contacts);
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("pages/listContact.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * search a contact by criteria
	 * 
	 * @param context
	 * @param critere
	 * @param key
	 * @return
	 */
	public List<Contact> requestByCriteria(DAOContact dao, String critere,
			String key) {

		List<Contact> contacts = new ArrayList<Contact>();
		List<Contact> results = new ArrayList<Contact>();
		// in the case of all contact search: no criteria
		if (critere == null || critere.isEmpty() || key == null
				|| key.isEmpty())
			return dao.search();

		// search by criteria
		if (critere.equals("ID")) {
			Contact contact = dao.searchById(Integer.parseInt(key));
			if (contact != null && !(contact instanceof Entreprise))
				contacts.add(contact);
		} else if (critere.equals("FirstName")) {
			contacts = dao.searchByName(key);

		} else if (critere.equals("Email")) {
			contacts = dao.searchByEmail(key);
		} else if (critere.equals("tel")) {

		}
		for (Contact contact : contacts) {
			if (!(contact instanceof Entreprise))
				results.add(contact);
		}
		return results;

	}

}
